package org.abbett.eskine.dao.inmemory;

import java.util.List;
import java.util.ArrayList;
import java.util.UUID;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import org.abbett.eskine.dao.PageDao;
import org.abbett.eskine.entity.Page;

@Component("pageDao")
public class InMemoryPageDao implements PageDao {
    private Map<Object, Page> primaryIndex = new LinkedHashMap<Object, Page>();

    @Override
    public void create(Page page) {
        Object pk = page.primaryKey();
        if (pk == null) {
            pk = generatePrimaryKey();
            
            page.setId((String) pk);
        }

        primaryIndex.put(pk, page);
    }

    @PostConstruct
    public void init() {
        // initialize the db with some initial data
    }

    /**
     * The "Create" or "Update" semantics of "CRUD"
     */
    public void createOrUpdate(Page page) {
        Object pk = page.primaryKey();
        if (pk == null || !primaryIndex.containsKey(pk)) {
            create(page);
        }
        else {
            update(page);
        }
    }

    /**
     * Find by Primary key
     * 
     * The "read" operation of CRUD
     */
    public Page findByPrimaryKey(String key) {
        return primaryIndex.get(key);
    }


    @Override
    public void delete(Page page) {
        primaryIndex.remove(page.primaryKey());
    }

    @Override
    public void update(Page page) {
        primaryIndex.put(page.primaryKey(), page);
    }


    /**
     * All Pages. 
        				All pages.
        			
     * 
     * The "read" operation of CRUD
     */
    public List<Page> findPages(Map<String, Object> params) {
        // TODO: implement your code here
        return new ArrayList<Page>(primaryIndex.values());
    }

    /**
     * Pages By User. 
                        All issues belonging to the user.
                    
     * 
     * The "read" operation of CRUD
     */
    public List<Page> findUserPages(Map<String, Object> params) {
        // TODO: implement your code here
        return new ArrayList<Page>(primaryIndex.values());
    }
    
    /**
     * Page. 
						A page, referenced by its identifier.
					
     * 
         * The "read" operation of CRUD
     */
    public Page getPage(Map<String, Object> params) {
        // TODO: implement your code here, by default this returns the first one
        if (primaryIndex.keySet().size() > 0) {
            return primaryIndex.get(primaryIndex.keySet().iterator().next());
        }
        
        return null;
    }

    @Override
    public Page newInstance() {
        return new InMemoryPage();
    }
    

    /**
     * A scheme for generating new primary keys
     * 
     * @return
     *
     * @author Vineet Manohar
     */
    public Object generatePrimaryKey() {
        return UUID.randomUUID().toString();   
    }
}// clickframes::version=1684127760::clickframes